package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.NewActivityPlanDao;
import com.qdlc.p2p.dal.dto.NewActivityPlan;
import com.qdlc.p2p.dal.model.NewActivityPlanModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Repository("newActivityPlanDao")
public class NewActivityPlanDaoImpl extends BaseDaoImpl<NewActivityPlan> implements NewActivityPlanDao {

    /**
     * 通过活动方案名称得到活动方案
     *
     * @param tache：方案名称
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public NewActivityPlan getActivityPlanByTache(String tache) {
        String jpql = "from NewActivityPlan where tache = :tache and status=:status";
        Query query = em.createQuery(jpql);
        query.setParameter("tache", tache);
        query.setParameter("status", 0);
        List<NewActivityPlan> list = query.getResultList();
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 查询活动方案列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<NewActivityPlanModel> list(NewActivityPlanModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(),
                model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName())) {
            param.addParam("name", SearchFilter.Operators.LIKE, model.getSearchName());
        }
        PageDataList<NewActivityPlan> dataList = this.findPageList(param);
        PageDataList<NewActivityPlanModel> modelDataList = new PageDataList<NewActivityPlanModel>();
        List<NewActivityPlanModel> list = new ArrayList<NewActivityPlanModel>();
        if (dataList.getList() != null && dataList.getList().size() > 0) {
            for (NewActivityPlan entity : dataList.getList()) {
                NewActivityPlanModel newActivityPlanModel = NewActivityPlanModel.instance(entity);
                //将修改人名字设置给model
                if (null != entity.getModifier())
                    newActivityPlanModel.setModifierName(entity.getModifier().getUserName());
                //把创建人名字设置给model
                if (null != entity.getCreator())
                    newActivityPlanModel.setCreatorName(entity.getCreator().getUserName());
                list.add(newActivityPlanModel);
            }
        }
        modelDataList.setPage(dataList.getPage());
        modelDataList.setList(list);
        return modelDataList;
    }

    /**
     * 更新抽奖规则状态
     *
     * @param model
     */
    @Override
    public int toggleStatus(NewActivityPlanModel model) {
        String jpql = "update NewActivityPlan set status = ?1, modifier = ?2, modifyTime = ?3 where id = ?4";
        Query query = em.createQuery(jpql);
        query.setParameter(1, model.getStatus());
        query.setParameter(2, model.getModifier());
        query.setParameter(3, model.getModifyTime());
        query.setParameter(4, model.getId());
        return query.executeUpdate();
    }

    /**
     * 查找可以被添加的活动
     * @return
     */
    @Override
    public List<NewActivityPlan> isCanBeAdd(NewActivityPlanModel model) {
        String jpql = "from NewActivityPlan where status = :status and tache = :tache";
        Query query = em.createQuery(jpql);
        query.setParameter("status", NewActivityPlanModel.ACTIVITY_PLAN_ENABLE);
        query.setParameter("tache", model.getTache());
        return query.getResultList();
    }

    /**
     * 查询开启的活动方案
     * @return
     */
    @Override
    public List<NewActivityPlan> findEnablePlan() {
        QueryParam param = QueryParam.getInstance();
        param.addParam("status", 0);
        return this.findByCriteria(param);
    }
}
